package co.edu.usbcali.hibernate.managebean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;


import co.edu.usbcali.hibernate.logica.ClienteLogica;
import co.edu.usbcali.hibernate.logica.IClienteLogica;
import co.edu.usbcali.hibernate.logica.IMascotaLogica;
import co.edu.usbcali.hibernate.logica.IVacunasMascotasLogica;
import co.edu.usbcali.hibernate.logica.MascotaLogica;
import co.edu.usbcali.hibernate.logica.VacunaLogica;
import co.edu.usbcali.hibernate.logica.VacunaMascotaLogica;
import co.edu.usbcali.hibernate.modelo.Clientes;
import co.edu.usbcali.hibernate.modelo.Mascotas;
import co.edu.usbcali.hibernate.modelo.Vacunas;
import co.edu.usbcali.hibernate.modelo.VacunasMascotasId;
import co.edu.usbcali.hibernate.utilidades.FacesUtils;
import co.edu.usbcali.hibernate.utilidades.Utilities;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;

public class VacunaMascotaView {
	
	private HtmlCommandButton btnAceptar;
	private HtmlCommandButton btnSalir;
	private HtmlCommandButton btnGuardar;
	private HtmlOutputText lblVacuna;
	private HtmlOutputText lblMascota;
	private HtmlOutputText lblFecha;
	private HtmlSelectOneMenu cmbVacuna;
	private HtmlSelectOneMenu cmbMascota;
	private SelectInputDate date;
	private List<SelectItem> mascotas2 = new ArrayList<SelectItem>();
	private List<SelectItem> vacunas2 = new ArrayList<SelectItem>();
	private List<Mascotas> mascotas;
	private List<Vacunas> vacunas;
	private boolean pnuVisible;
	
	
	public HtmlCommandButton getBtnSalir() {
		return btnSalir;
	}
	public void setBtnSalir(HtmlCommandButton btnSalir) {
		this.btnSalir = btnSalir;
	}
	public SelectInputDate getDate() {
		return date;
	}
	public void setDate(SelectInputDate date) {
		this.date = date;
	}

	
	
	
	public VacunaMascotaView() {
		
		try {
			
			MascotaLogica mascotaLogica = new MascotaLogica();
			mascotas = mascotaLogica.consultarMascotas();
			SelectItem item = new SelectItem(0,"Seleccione una Mascota");
			mascotas2.add(item);
			for (Mascotas mascota : mascotas) {
				item = new SelectItem(mascota.getMasCodigo(), mascota.getMasNombre());
				mascotas2.add(item);
			}
			VacunaLogica logica = new VacunaLogica();
			vacunas = logica.consultarVacunas();
			item = new SelectItem(0,"Seleccione una Vacuna");
			vacunas2.add(item);
			for (Vacunas vacuna : vacunas) {
				item = new SelectItem(vacuna.getVacCodigo(), vacuna.getVacNombre());
				vacunas2.add(item);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public HtmlCommandButton getBtnAceptar() {
		return btnAceptar;
	}
	public void setBtnAceptar(HtmlCommandButton btnAceptar) {
		this.btnAceptar = btnAceptar;
	}
	public HtmlCommandButton getBtnGuardar() {
		return btnGuardar;
	}
	public void setBtnGuardar(HtmlCommandButton btnGuardar) {
		this.btnGuardar = btnGuardar;
	}
	public HtmlOutputText getLblVacuna() {
		return lblVacuna;
	}
	public void setLblVacuna(HtmlOutputText lblVacuna) {
		this.lblVacuna = lblVacuna;
	}
	public HtmlOutputText getLblMascota() {
		return lblMascota;
	}
	public void setLblMascota(HtmlOutputText lblMascota) {
		this.lblMascota = lblMascota;
	}
	public HtmlOutputText getLblFecha() {
		return lblFecha;
	}
	public void setLblFecha(HtmlOutputText lblFecha) {
		this.lblFecha = lblFecha;
	}
	public HtmlSelectOneMenu getCmbVacuna() {
		return cmbVacuna;
	}
	public void setCmbVacuna(HtmlSelectOneMenu cmbVacuna) {
		this.cmbVacuna = cmbVacuna;
	}
	public HtmlSelectOneMenu getCmbMascota() {
		return cmbMascota;
	}
	public void setCmbMascota(HtmlSelectOneMenu cmbMascota) {
		this.cmbMascota = cmbMascota;
	}
	public List<SelectItem> getMascotas2() {
		return mascotas2;
	}
	public void setMascotas2(List<SelectItem> mascotas2) {
		this.mascotas2 = mascotas2;
	}
	public List<SelectItem> getVacunas2() {
		return vacunas2;
	}
	public void setVacunas2(List<SelectItem> vacunas2) {
		this.vacunas2 = vacunas2;
	}
	public List<Mascotas> getMascotas() {
		return mascotas;
	}
	public void setMascotas(List<Mascotas> mascotas) {
		this.mascotas = mascotas;
	}
	public List<Vacunas> getVacunas() {
		return vacunas;
	}
	public void setVacunas(List<Vacunas> vacunas) {
		this.vacunas = vacunas;
	}
	public boolean isPnuVisible() {
		return pnuVisible;
	}
	public void setPnuVisible(boolean pnuVisible) {
		this.pnuVisible = pnuVisible;
	}
	
	public String salir_action() throws Exception {
		return "menu";
	}

	public void guardar_action(ActionEvent e) throws Exception {
		Long mascota = Long.parseLong(cmbMascota.getValue().toString());
		Long vacuna = Long.parseLong(cmbVacuna.getValue().toString());
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		Date dates = (Date) date.getValue();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dates);
		calendar.add(calendar.DAY_OF_YEAR, 1);
		dates = calendar.getTime();
		System.out.println(dates);
		String string = format.format(dates);
		dates = format.parse(string);
		VacunasMascotasId id = new VacunasMascotasId(mascota, vacuna);
		IVacunasMascotasLogica logica = new VacunaMascotaLogica();
		logica.crearVacunasMascotas(id,dates);
		cmbMascota.setValue(0);
		cmbVacuna.setValue(0);
		
		

	}
	
	public void aceptar_action(ActionEvent e) throws Exception {
		pnuVisible = false;
	}
}
